// pages/index/lender/sharedFlats/sharedFlats.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    attention:0,
    autoplay: true,
    duration: 500,
    interval: 3000,
    swiperList: [
      '/images/banner.png',
      '/images/banner1.png',
      '/images/banner2.png',
    ],
    markers: [{
      iconPath: "/images/dw.png",
      id: 0,
      latitude: 34.836239,
      longitude: 113.565104,
      width: 50,
      height: 50 // 浮标高度
    }],
    latitude: 34.836239,
    longitude: 113.565104,
    comment:'',
    topicId:0,
    topicMessage:{},
    userId:0,
  },
  onAttention(){
    if(this.data.attention === 0){
        this.setData({
            attention:1
        })
        this.attention()
    }else{
        this.setData({
            attention:0
        })
        this.cancelAttention()
    }
  },
  attention(){
    wx.request({
        url: 'http://localhost:8080/ihome/topic/attentiontopic',
        method:'GET',
        data:{
            userId:this.data.userId,
            topicId:this.data.topicId,  
        },
        success:(resp)=>{
            console.log(resp)
        }
    })
  },
  cancelAttention(){
    wx.request({
        url: 'http://localhost:8080/ihome/topic/cancelattention',
            method:'GET',
            data:{
                userId:this.data.userId,
                topicId:this.data.topicId, 
            },
            success:(resp)=>{
                console.log(resp)
            }
        })
  },
  getMessage(){
    wx.request({
        url: 'http://localhost:8080/ihome/topic/selectsharedflatstopicdetails',
        method:"GET",
        data:{
            userId:this.data.userId,
            topicId:this.data.topicId
        },
        success:(resp)=>{
            this.setData({
                topicMessage:resp.data.data
            })
            if(this.data.topicMessage.collectId != null && this.data.topicMessage.collectId != ""){
                this.setData({
                    attention:1
                })
            }
            console.log(this.data.topicMessage)
        }
    })
  },
  // 处理输入事件，实时更新comment变量  
  handleInput: function (e) {
    this.setData({
      comment: e.detail.value // 更新评论内容  
    });
  },
  sendComment(){
    wx.request({
        url: 'http://localhost:8080/ihome/topicComment/insertihtopiccomment',
        method:'POST',
        data:{
            content:this.data.comment,
            topicId:this.data.topicId,
            userId:this.data.userId,
        },
        success:(resp)=>{
            console.log(resp)
        }
    })
  },
  // 处理发送按钮的点击事件  
  handleSendComment: function () {
    const {comment} = this.data; // 获取 comment 的值  
    if (comment) {
      console.log('评论内容:', comment); // 打印输入内容
      this.sendComment()
      wx.redirectTo({
        url: '/pages/index/lender/sharedFlats/sharedFlats?id='+this.data.topicId,
      })
      // 发送成功后清空输入框  
      this.setData({
        comment: ''
      }); // 清空输入框  
    } else {
      console.log('评论内容为空'); // 输入为空时的处理  
    }
  },

  // 处理输入框确认事件  
  handleCommentConfirm: function (e) {
    const comment = e.detail.value; // 获取输入的评论内容  
    if (comment) {
      console.log('评论内容:', comment); // 打印评论内容  
      this.setData({
        comment: ''
      }); // 清空输入框  
    } else {
      console.log('评论内容为空'); // 输入为空时的处理  
    }
  },

  handleFocus: function () {
    console.log('输入框获得焦点');
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getUserInfo(){
    var value = wx.getStorageSync('userInfo')
    console.log('用户信息',value)
    this.setData({
        userId:value.userId
    })
  },
  onLoad(options) {
      this.getUserInfo()
      this.setData({
        topicId:options.id
      })
    this.getMessage()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})